﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Globalization;

using BizElements.Core;

namespace BizElements.BusinessLayer
{
    /// <summary>
    /// Entity model class for the table/view 'AppSettings'.
    /// </summary>
    [Serializable]
    class AppSettingsEntity : EntityModelBase, IAppSetting
    {
        #region Private members.

        // Members mapped to database columns.
        private int? appSettingId;
        private string actorId;
        private string application;
        private string module;
        private string context;
        private string component;
        private string propertyPathString;
        private string value;
        private string description;
        private DateTime? validFrom;
        private DateTime? validUntil;
        private byte? status;


        #endregion

        #region Protected properties.

        // Properties mapped to database columns.    
        protected int? _AppSettingId
        {
            get
            {
                return this.appSettingId;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if ((!value.HasValue != !this.appSettingId.HasValue) || (value.HasValue && (value.Value != this.appSettingId.Value)))
                        this.EntityState = EntityState.OutOfSync;
                }
                this.appSettingId = value;
            }
        }

        protected string _ActorId
        {
            get
            {
                return this.actorId;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.actorId)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.actorId = value;
            }
        }

        protected string _Application
        {
            get
            {
                return this.application;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.application)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.application = value;
            }
        }

        protected string _Module
        {
            get
            {
                return this.module;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.module)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.module = value;
            }
        }

        protected string _Context
        {
            get
            {
                return this.context;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.context)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.context = value;
            }
        }

        protected string _Component
        {
            get
            {
                return this.component;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.component)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.component = value;
            }
        }

        protected string _PropertyPathString
        {
            get
            {
                return this.propertyPathString;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.propertyPathString)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.propertyPathString = value;
            }
        }

        protected string _Value
        {
            get
            {
                return this.value;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.value)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.value = value;
            }
        }

        protected string _Description
        {
            get
            {
                return this.description;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if (value != this.description)
                        this.EntityState = EntityState.OutOfSync;
                }
                this.description = value;
            }
        }

        protected DateTime? _ValidFrom
        {
            get
            {
                return this.validFrom;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if ((!value.HasValue != !this.validFrom.HasValue) || (value.HasValue && (value.Value != this.validFrom.Value)))
                        this.EntityState = EntityState.OutOfSync;
                }
                this.validFrom = value;
            }
        }

        protected DateTime? _ValidUntil
        {
            get
            {
                return this.validUntil;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if ((!value.HasValue != !this.validUntil.HasValue) || (value.HasValue && (value.Value != this.validUntil.Value)))
                        this.EntityState = EntityState.OutOfSync;
                }
                this.validUntil = value;
            }
        }

        protected byte? _Status
        {
            get
            {
                return this.status;
            }
            set
            {
                if (this.EntityState == EntityState.Synchronized)
                {
                    if ((!value.HasValue != !this.status.HasValue) || (value.HasValue && (value.Value != this.status.Value)))
                        this.EntityState = EntityState.OutOfSync;
                }
                this.status = value;
            }
        }


        #endregion

        #region Constructors.

        /// <summary>
        /// Default constructor.
        /// </summary>
        public AppSettingsEntity()
            : base(new AppSettingsMeta())
        {
        }

        /// <summary>
        /// Constructor. Initalizes Table property with the specified (shared) instance.
        /// </summary>
        /// <param name="dbTable">Metadata for table/view to which the entity belongs to.</param>
        public AppSettingsEntity(AppSettingsMeta dbTable)
            : base(dbTable)
        {
        }

        /// <summary>
        /// Constructor. Initializes primary key. Useful for methods that don't require other values (eg. DeleteOne, SelectOne).
        /// </summary>
        public AppSettingsEntity(int appSettingId)
            : base(new AppSettingsMeta())
        {
            _AppSettingId = appSettingId;
        }

        /// <summary>
        /// Constructor. Initializes primary key. Useful for methods that don't require other values (eg. DeleteOne, SelectOne).
        /// </summary>
        public AppSettingsEntity(AppSettingsMeta dbTable, int appSettingId)
            : base(dbTable)
        {
            _AppSettingId = appSettingId;
        }

        /// <summary>
        /// Constructor. Initializes members with the values in the given DataRow.
        /// </summary>
        /// <param name="row">DataRow with required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public AppSettingsEntity(DataRow row)
            : base(new AppSettingsMeta())
        {
            FromDataRow(row);
        }

        /// <summary>
        /// Constructor. Initializes members with the values in the given DataRow.
        /// </summary>
        /// <param name="dbTable">Metadata for table/view to which the entity belongs to.</param>
        /// <param name="row">DataRow with required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public AppSettingsEntity(AppSettingsMeta dbTable, DataRow row)
            : base(dbTable)
        {
            FromDataRow(row);
        }

        /// <summary>
        /// Copy constructor.
        /// </summary>
        /// <param name="existing">Instance of AppSettingsEntity.</param>
        public AppSettingsEntity(AppSettingsEntity existing)
            : base(existing.Table)
        {
            FromExistingEntity(existing);
        }

        static readonly Type DbTableClass = typeof(AppSettingsMeta);

        /// <summary>
        /// Initializes all members of AppSettingsEntity and base EntityModelBase class. This is the fastest way to initialize entity with data from database.
        /// </summary>
        private AppSettingsEntity(IDbTable table, EntityState entityState, int? appSettingId, string actorId, string application, string module, string context, string component, string propertyPathString, string value, string description, DateTime? validFrom, DateTime? validUntil, byte? status)
            : base(table, DbTableClass, entityState)
        {
            this.appSettingId = appSettingId;
            this.actorId = actorId;
            this.application = application;
            this.module = module;
            this.context = context;
            this.component = component;
            this.propertyPathString = propertyPathString;
            this.value = value;
            this.description = description;
            this.validFrom = validFrom;
            this.validUntil = validUntil;
            this.status = status;
        }

        /// <summary>
        /// Initializes all members mapped to fields.
        /// </summary>
        protected void Init(int? appSettingId, string actorId, string application, string module, string context, string component, string propertyPathString, string value, string description, DateTime? validFrom, DateTime? validUntil, byte? status)
        {
            _AppSettingId = appSettingId;
            _ActorId = actorId;
            _Application = application;
            _Module = module;
            _Context = context;
            _Component = component;
            _PropertyPathString = propertyPathString;
            _Value = value;
            _Description = description;
            _ValidFrom = validFrom;
            _ValidUntil = validUntil;
            _Status = status;
        }

        #endregion

        #region Methods.

        /// <summary>
        /// Gets the value(s) that uniquely identifiy an entity.
        /// In the order as specified in accompanying IDbTable metadata class.
        /// NULL if the parent table/view doesn't have a primary key constraint or the required fields are not set.
        /// </summary>
        public override object[] GetPrimaryKeyValue()
        {
            if (_AppSettingId != null)
                return new object[] { _AppSettingId.Value };
            else
                return null;
        }

        /// <summary>
        /// Initializes entity members with data stored in the given DataRow.
        /// </summary>
        /// <param name="row">DataRow with the required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public override void FromDataRow(DataRow row)
        {
            object currentColumnValue;

            currentColumnValue = TryGetColumnValue(row, "AppSettingId");
            _AppSettingId = (currentColumnValue != DBNull.Value) ? (int?)Convert.ToInt32(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "ActorId");
            _ActorId = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Application");
            _Application = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Module");
            _Module = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Context");
            _Context = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Component");
            _Component = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "PropertyPathString");
            _PropertyPathString = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Value");
            _Value = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Description");
            _Description = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "ValidFrom");
            _ValidFrom = (currentColumnValue != DBNull.Value) ? (DateTime?)Convert.ToDateTime(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "ValidUntil");
            _ValidUntil = (currentColumnValue != DBNull.Value) ? (DateTime?)Convert.ToDateTime(currentColumnValue) : null;

            currentColumnValue = TryGetColumnValue(row, "Status");
            _Status = (currentColumnValue != DBNull.Value) ? (byte?)Convert.ToByte(currentColumnValue) : null;

            bool setEntityStateToSynchronized = (row.RowState == DataRowState.Unchanged);
            if (setEntityStateToSynchronized)
                this.EntityState = EntityState.Synchronized;
        }

        private static object TryGetColumnValue(DataRow row, string columnName)
        {
            int colIdx = row.Table.Columns.IndexOf(columnName);
            return (colIdx >= 0) ? row[colIdx] : null;
        }

        private static object GetColumnValue(DataRow row, IDbColumn column)
        {
            int colIdx = row.Table.Columns.IndexOf(column.Alias);
            if (colIdx < 0)
                colIdx = row.Table.Columns.IndexOf(column.ColumnName);
            if (colIdx < 0)
                throw new ArgumentException("DataTable doesn't contain the specified column (" + column.ColumnName + ").");

            return row[colIdx];
        }

        /// <summary>
        /// Initializes entity members with data stored in the given DataRow.
        /// </summary>
        /// <param name="row">DataRow with all or some of the columns defined in meta data.</param>
        /// <param name="fieldMetaData"><see cref="IDbTable"/> meta data object which links ADO.NET row columns to entity properties.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public override void FromDataRow(DataRow row, IDbTable fieldMetaData)
        {
            IDbColumn currentColumn;
            object currentColumnValue;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.AppSettingId.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _AppSettingId = (currentColumnValue != DBNull.Value) ? (int?)Convert.ToInt32(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.ActorId.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _ActorId = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Application.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Application = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Module.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Module = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Context.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Context = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Component.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Component = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.PropertyPathString.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _PropertyPathString = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Value.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Value = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Description.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Description = (currentColumnValue != DBNull.Value) ? (string)Convert.ToString(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.ValidFrom.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _ValidFrom = (currentColumnValue != DBNull.Value) ? (DateTime?)Convert.ToDateTime(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.ValidUntil.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _ValidUntil = (currentColumnValue != DBNull.Value) ? (DateTime?)Convert.ToDateTime(currentColumnValue) : null;

            currentColumn = fieldMetaData.Columns.GetByPropertyName(this.Table.Status.PropertyName);
            currentColumnValue = (currentColumn != null) ? GetColumnValue(row, currentColumn) : DBNull.Value;
            _Status = (currentColumnValue != DBNull.Value) ? (byte?)Convert.ToByte(currentColumnValue) : null;

            bool setEntityStateToSynchronized = (row.RowState == DataRowState.Unchanged);
            if (setEntityStateToSynchronized)
                this.EntityState = EntityState.Synchronized;
        }

        /// <summary>
        /// Converts the given DataRow to AppSettingsEntity.
        /// </summary>
        /// <param name="row">DataRow with required columns.</param>
        /// <remarks>If row's state is <see cref="DataRowState.Unchanged"/> the entity's state will be set to <see cref="EntityState.Synchronized"/>. Other states are ignored.</remarks>
        public static explicit operator AppSettingsEntity(DataRow row)
        {
            return new AppSettingsEntity(row);
        }

        /// <summary>
        /// Creates an array of objects containing entity data.
        /// </summary>
        /// <returns>Entity values.</returns>
        public override object[] ToObjectArray()
        {
            object[] values = new object[12];

            values[0] = (_AppSettingId != null) ? (object)_AppSettingId.Value : null;
            values[1] = (_ActorId != null) ? (object)_ActorId : null;
            values[2] = (_Application != null) ? (object)_Application : null;
            values[3] = (_Module != null) ? (object)_Module : null;
            values[4] = (_Context != null) ? (object)_Context : null;
            values[5] = (_Component != null) ? (object)_Component : null;
            values[6] = (_PropertyPathString != null) ? (object)_PropertyPathString : null;
            values[7] = (_Value != null) ? (object)_Value : null;
            values[8] = (_Description != null) ? (object)_Description : null;
            values[9] = (_ValidFrom != null) ? (object)_ValidFrom.Value : null;
            values[10] = (_ValidUntil != null) ? (object)_ValidUntil.Value : null;
            values[11] = (_Status != null) ? (object)_Status.Value : null;

            return values;
        }

        /// <summary>
        /// Initializes entity members with the given values.
        /// </summary>
        /// <param name="entityValues">Array with the required values.</param>
        public override void FromObjectArray(object[] entityValues)
        {
            _AppSettingId = (entityValues[0] != null) ? (int?)Convert.ToInt32(entityValues[0]) : null;
            _ActorId = (entityValues[1] != null) ? (string)Convert.ToString(entityValues[1]) : null;
            _Application = (entityValues[2] != null) ? (string)Convert.ToString(entityValues[2]) : null;
            _Module = (entityValues[3] != null) ? (string)Convert.ToString(entityValues[3]) : null;
            _Context = (entityValues[4] != null) ? (string)Convert.ToString(entityValues[4]) : null;
            _Component = (entityValues[5] != null) ? (string)Convert.ToString(entityValues[5]) : null;
            _PropertyPathString = (entityValues[6] != null) ? (string)Convert.ToString(entityValues[6]) : null;
            _Value = (entityValues[7] != null) ? (string)Convert.ToString(entityValues[7]) : null;
            _Description = (entityValues[8] != null) ? (string)Convert.ToString(entityValues[8]) : null;
            _ValidFrom = (entityValues[9] != null) ? (DateTime?)Convert.ToDateTime(entityValues[9]) : null;
            _ValidUntil = (entityValues[10] != null) ? (DateTime?)Convert.ToDateTime(entityValues[10]) : null;
            _Status = (entityValues[11] != null) ? (byte?)Convert.ToByte(entityValues[11]) : null;
        }

        /// <summary>
        /// Gets the parent entity defined by the given foreign key.
        /// </summary>
        /// <param name="foreignKey">FK which must be defined in the AppSettingsMeta class or an exception is generated.</param>
        /// <returns>Parent entity. NULL if the FK fields haven't been set or if the entity with the given key values doesn't exist.</returns>
        public override IEntity GetParent(DbRelation foreignKey)
        {
            throw new Exception("No foreign keys are defined in the AppSettingsMeta class.");
        }

        /// <summary>
        /// Sets the given value into the member that represents the parent entity defined by the foreign key.
        /// </summary>
        /// <param name="foreignKey">FK which must be defined in the CFunctionsMeta class or an exception is generated.</param>
        /// <param name="entity">Parent entity. May be NULL. Must be an instance of the CFunctionsEntity or a derived class.</param>
        public override void SetParent(DbRelation foreignKey, IEntity entity)
        {
            throw new Exception("No foreign keys are defined in the AppSettingsMeta class.");
        }

        #endregion

        #region Table.

        /// <summary>
        /// Gets typed IDbTable object for the entity's table/view.
        /// </summary>
        public AppSettingsMeta Table
        {
            get { return (AppSettingsMeta)_Table; }
        }

        #endregion

        #region Public properties mapped to database columns.

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'AppSettingId'.
        /// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
        /// </summary>
        public virtual int AppSettingId
        {
            get
            {
                if (_AppSettingId == null)
                {
                    if (this.NullCheckEnabled)
                        throw new NoNullAllowedException("AppSettingsEntity.get_AppSettingId: AppSettingId is not set yet.");
                    else
                        return default(int);
                }

                return _AppSettingId.Value;
            }
            set
            {
                _AppSettingId = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'ActorId'.
        /// </summary>
        public virtual string ActorId
        {
            get
            {
                return _ActorId;
            }
            set
            {
                _ActorId = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Application'.
        /// </summary>
        public virtual string Application
        {
            get
            {
                return _Application;
            }
            set
            {
                _Application = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Module'.
        /// </summary>
        public virtual string Module
        {
            get
            {
                return _Module;
            }
            set
            {
                _Module = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'Context'.
        /// </summary>
        public virtual string Context
        {
            get
            {
                return _Context;
            }
            set
            {
                _Context = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Component'.
        /// </summary>
        public virtual string Component
        {
            get
            {
                return _Component;
            }
            set
            {
                _Component = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'PropertyPathString'.
        /// </summary>
        public virtual string PropertyPathString
        {
            get
            {
                return _PropertyPathString;
            }
            set
            {
                _PropertyPathString = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'Value'.
        /// </summary>
        public virtual string Value
        {
            get
            {
                return _Value;
            }
            set
            {
                _Value = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'Description'.
        /// </summary>
        public virtual string Description
        {
            get
            {
                return _Description;
            }
            set
            {
                _Description = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'ValidFrom'.
        /// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
        /// </summary>
        public virtual DateTime ValidFrom
        {
            get
            {
                if (_ValidFrom == null)
                {
                    if (this.NullCheckEnabled)
                        throw new NoNullAllowedException("AppSettingsEntity.get_ValidFrom: ValidFrom is not set yet.");
                    else
                        return default(DateTime);
                }

                return _ValidFrom.Value;
            }
            set
            {
                _ValidFrom = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the nullable field 'ValidUntil'.
        /// </summary>
        public virtual DateTime? ValidUntil
        {
            get
            {
                return _ValidUntil;
            }
            set
            {
                _ValidUntil = value;
            }
        }

        /// <summary>
        /// Gets or sets the value which is mapped to the non-nullable field 'Status'.
        /// If null-check is enabled a NoNullAllowedException is thrown if getter is used before the value has been set.
        /// </summary>
        public virtual byte Status
        {
            get
            {
                if (_Status == null)
                {
                    if (this.NullCheckEnabled)
                        throw new NoNullAllowedException("AppSettingsEntity.get_Status: Status is not set yet.");
                    else
                        return default(byte);
                }

                return _Status.Value;
            }
            set
            {
                _Status = value;
            }
        }

        #endregion

        #region NewEntity static methods suitable for usage in EntityBuilder<T>.

        /// <summary>Creates new entity and initializes all members of with the given values.</summary>
        /// <param name="table">Metadata for table/view to which the entity belongs to.</param>
        /// <param name="entityState">Indicates the state of entity with regard to data-source.</param>
        /// <param name="values">Array which contains values for all properties mapped to database columns in the following order: AppSettingId, ActorId, Application, Module, Context, Component, PropertyPathString, Value, Description, ValidFrom, ValidUntil, Status.</param>
        /// <remarks>This is the fastest method to initialize an entity as it directly initializes all members of base and derived class, 
        /// skips all validation checks and doesn't attempt to convert provided value data types. The method is typically used in combination 
        /// with IDataConverter objects which retrieve property values directly from IDataReader objects.</remarks>
        public static AppSettingsEntity NewEntity(IDbTable table, EntityState entityState, object[] values)
        {
            return new AppSettingsEntity(table, entityState, (int)values[0], (string)ReplaceDbNull(values[1]), (string)values[2], (string)values[3], (string)ReplaceDbNull(values[4]), (string)values[5], (string)ReplaceDbNull(values[6]), (string)ReplaceDbNull(values[7]), (string)ReplaceDbNull(values[8]), (DateTime)values[9], CastTo<DateTime>(values[10]), (byte)values[11]);
        }

        /// <summary>Creates new entity and initializes members for which values are defined in the array.</summary>
        /// <param name="table">Metadata for table/view to which the entity belongs to.</param>
        /// <param name="entityState">Indicates the state of entity with regard to data-source.</param>
        /// <param name="values">Array which contains values or nulls for all properties mapped to database columns in the following order: AppSettingId, ActorId, Application, Module, Context, Component, PropertyPathString, Value, Description, ValidFrom, ValidUntil, Status.</param>
        /// <remarks>This is the fastest method to initialize an entity as it directly initializes all members of base and derived class, 
        /// skips all validation checks and doesn't attempt to convert provided value data types. The method is typically used in combination 
        /// with IObjectReader objects which retrieve property values directly from IDataReader objects.</remarks>
        public static AppSettingsEntity NewPartialEntity(IDbTable table, EntityState entityState, object[] values)
        {
            return new AppSettingsEntity(table, entityState, CastTo<int>(values[0]), (string)ReplaceDbNull(values[1]), (string)ReplaceDbNull(values[2]), (string)ReplaceDbNull(values[3]), (string)ReplaceDbNull(values[4]), (string)ReplaceDbNull(values[5]), (string)ReplaceDbNull(values[6]), (string)ReplaceDbNull(values[7]), (string)ReplaceDbNull(values[8]), CastTo<DateTime>(values[9]), CastTo<DateTime>(values[10]), CastTo<byte>(values[11]));
        }

        private static T? CastTo<T>(object val)
            where T : struct
        {
            if (IsNull(val))
                return null;
            else
                return (T)val;
        }

        private static bool IsNull(object val)
        {
            return (val == DBNull.Value || val == null);
        }

        private static object ReplaceDbNull(object dbValue)
        {
            return (dbValue != DBNull.Value) ? dbValue : null;
        }

        #endregion

        // Custom: IAppSetting members.

        object IAppSetting.ActorId
        {
            get { return this.ActorId; }
            set { this.ActorId = (value != null) ? value.ToString() : null; }
        }

        object IAppSetting.Value
        {
            get { return this.Value; }
            set { this.Value = (value != null) ? Convert.ToString(value, CultureInfo.InvariantCulture) : null; }
        }

        /// <summary>
        /// Gets or sets the elements, ie. properties and indexes, of the <see cref="PropertyPathString"/>.
        /// </summary>
        /// <value>Array of strings. Empty array if the value is assigned directly to the component.
        /// <b>null</b> does not represent a valid value.</value>
        public virtual string[] PropertyPath
        {
            get { return string.IsNullOrEmpty(this.PropertyPathString) ? new string[0] : this.PropertyPathString.Split('/'); }
            set
            {
                foreach (string segment in value)
                    this.PropertyPathString += segment + "/";

                if (this.PropertyPathString.Length > 0)
                    this.PropertyPathString = this.PropertyPathString.Remove(this.PropertyPathString.Length - 1);
            }
        }
    }
}
